class PageObserver < ActiveRecord::Observer
  
  def after_create(page)
    update_home_page(page)  
  end

  def after_update(page)
    update_home_page(page)
  end
 
  private
  
  def update_home_page(page)
    
    if page.is_home
      #find the other that is home
      old_home_pages = Page.find_all_by_is_home(true)
      for old_home_page in old_home_pages
        if page.id != old_home_page.id 
          old_home_page.is_home =false
          old_home_page.save
        end
      end
    end
    
  end
end